# -*- coding: UTF-8 -*-
import json
from utils import request


class Client(object):
    def __init__(self, lark_host):
        """
        初始化客户端
        :param lark_host: 飞书 API 的主机地址
        """
        self._host = lark_host

    def get_tenant_access_token(self, app_id, app_secret):
        """
        获取租户级别的访问令牌
        :param app_id: 应用 ID
        :param app_secret: 应用密钥
        :return: 租户访问令牌
        """
        url = self._host + "/open-apis/auth/v3/app_access_token/internal/"
        headers = {
            'Content-Type': 'application/json; charset=utf-8'
        }
        payload = {
            'app_id': app_id,
            'app_secret': app_secret
        }
        resp = request("POST", url, headers, payload)
        return resp['tenant_access_token']

    def get_user_access_token(self, tenant_access_token, code):
        """
        获取用户级别的访问令牌
        :param tenant_access_token: 租户访问令牌
        :param code: 授权码
        :return: 用户访问令牌
        """
        url = self._host + "/open-apis/authen/v1/access_token"
        headers = {
            'Content-Type': 'application/json; charset=utf-8'
        }
        payload = {
            "grant_type": "authorization_code",
            "code": code,
            "app_access_token": tenant_access_token
        }
        resp = request("POST", url, headers, payload)
        return resp['data']['access_token']

    def get_root_folder_token(self, access_token):
        """
        获取根文件夹的 token
        :param access_token: 用户访问令牌
        :return: 根文件夹 token
        """
        url = self._host + "/open-apis/drive/explorer/v2/root_folder/meta"
        headers = {
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer ' + access_token
        }
        resp = request("GET", url, headers)
        return resp['data']['token']

    def create_spreadsheet(self, access_token, foldertoken, title):
        """
        创建一个新的电子表格
        :param access_token: 用户访问令牌
        :param foldertoken: 文件夹 token
        :param title: 电子表格标题
        :return: 电子表格 token 和 URL
        """
        url = self._host + "/open-apis/sheets/v3/spreadsheets"
        headers = {
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer ' + access_token
        }
        payload = {
            "title": title,
            "folder_token": foldertoken
        }
        resp = request("POST", url, headers, payload)
        return resp['data']['spreadsheet']['spreadsheet_token'], resp['data']['spreadsheet']['url']

    def get_first_sheetid(self, access_token, doctoken):
        """
        获取第一个工作表的 ID
        :param access_token: 用户访问令牌
        :param doctoken: 电子表格 token
        :return: 第一个工作表的 ID
        """
        url = self._host + "/open-apis/sheets/v2/spreadsheets/" + doctoken + "/metainfo"
        headers = {
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer ' + access_token
        }
        resp = request("GET", url, headers)
        return resp['data']['sheets'][0]["sheetId"]

    def batch_update_values(self, access_token, doctoken, data):
        """
        批量更新电子表格中的值
        :param access_token: 用户访问令牌
        :param doctoken: 电子表格 token
        :param data: 要更新的数据
        :return: 电子表格 token
        """
        url = self._host + "/open-apis/sheets/v2/spreadsheets/" + doctoken + "/values_batch_update"
        headers = {
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer ' + access_token
        }
        payload = data
        resp = request("POST", url, headers, payload)
        return resp['data']['spreadsheetToken']

    def batch_update_styles(self, access_token, doctoken, data):
        """
        批量设置单元格样式
        :param access_token: 用户访问令牌
        :param doctoken: 电子表格 token
        :param data: 要更新的样式数据
        :return: 电子表格 token
        """
        url = self._host + "/open-apis/sheets/v2/spreadsheets/" + doctoken + "/styles_batch_update"
        headers = {
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer ' + access_token
        }
        payload = data
        resp = request("PUT", url, headers, payload)
        return resp['data']['spreadsheetToken']

    def add_permissions_member(self, access_token, doctoken, doctype, member_type, member_id, perm):
        """
        添加电子表格的权限成员
        :param access_token: 用户访问令牌
        :param doctoken: 电子表格 token
        :param doctype: 文档类型
        :param member_type: 成员类型
        :param member_id: 成员 ID
        :param perm: 权限类型
        参考API文档：https://open.feishu.cn/document/server-docs/docs/permission/permission-member/create
        """
        url = self._host + "/open-apis/drive/v1/permissions/" + doctoken + "/members?type=" + doctype + "&need_notification=false"
        headers = {
            'Content-Type': 'application/json; charset=utf-8',
            'Authorization': 'Bearer ' + access_token
        }
        payload = {
            "member_type": member_type,
            "member_id": member_id,
            "perm": perm
        }
        request("POST", url, headers, payload)
